﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DevExpress.Web.Mvc;
using System.Data;
using DevExpress.Web.ASPxGridView;
using EnterpriseFund.Common;
using DevExpress.XtraReports.UI;
using DevExpress.XtraPrinting;

namespace EnterpriseFund.Controllers
{
    public class AllowanceFileQueryController : Common.AdminBaseController
    {
        //
        // GET: /AllowanceFileQuery/

        public ActionResult Index()
        {
            BLL.FinanceProp FinancePropBLL = new BLL.FinanceProp();
            ViewData["Year"] = OperateSession.get(Common.GlobalConstant.PARAM_SYSYEAR);
            ViewData["FinancePropList"] = FinancePropBLL.GetModelList("");
            return View();
        }

        public ActionResult AllowanceFileQueryPartial()
        {
            DataSet ds = GetAllData();
            return PartialView("AllowanceFileQueryPartial", ds.Tables[0]);
        }

        private DataSet GetAllData()
        {
            int pageIndex = Request.Params["pageIndex"] == null ? 0 : int.Parse(Request.Params["pageIndex"].ToString());
            int pageCount = Request.Params["PageCount"] == null ? 0 : int.Parse(Request.Params["PageCount"].ToString());
            int pageRowSize = Request.Params["pageRowSize"] == null ? 0 : int.Parse(Request.Params["pageRowSize"].ToString());
            int pageRowCount = Request.Params["pageRowCount"] == null ? 0 : int.Parse(Request.Params["pageRowCount"].ToString());
            string callbackArg = DevExpressHelper.CallbackArgument;

            string fileNo = Request.Params["fileNo"];
            string nsrmc = Request.Params["nsrmc"];
            string financePropNo = Request.Params["financePropNo"];
            string fileName = Request.Params["fileName"];
            string amount1 = Request.Params["amount1"];
            string amount2 = Request.Params["amount2"];
            string FYear = Request.Params["FYear"];
            string strwhere = " 1=1 ";
            if (fileNo != "")
            {
                strwhere += " and FFileNo like  '%" + fileNo + "%'";
            }


            if (nsrmc != "")
            {
                strwhere += " and NSRMC like  '%" + nsrmc + "%'";
            }

            if (fileName != "")
            {
                strwhere += " and FFileName like  '%" + fileName + "%'";
            }

            if (FYear != "")
            {
                strwhere += " and FYear =  " + FYear ;
            }



            if (financePropNo != "")
            {
                strwhere += " and a.FFinancePropNo ='" + financePropNo + "'";
            }

            if (amount1 != "" && amount2 != "")
            {
                strwhere += " and FAmount >=" + amount1 + " and FAmount<=" + amount2 + " ";
            }
            else if (amount1 != "")
            {
                strwhere += " and FAmount >=" + amount1;
            }
            else if (amount2 != "")
            {
                strwhere += " and FAmount <=" + amount2;
            }


            BLL.AllowanceFileEntry allowanceFileEntryBLL = new BLL.AllowanceFileEntry();
            DataSet ds = allowanceFileEntryBLL.GetALList(strwhere);
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                r["FAmount"] = decimal.Parse(r["FAmount"].ToString()) / 10000;
            }
            return ds;
        }

        #region 导出EXCEL
        public ActionResult ReportViewerPartial()
        {
           // var strWhere = GetWhereString();
            var zsxx = GetAllData().Tables[0];
            MVCReportGeneratonHelper generator = new MVCReportGeneratonHelper();
            generator.CustomizeColumnsCollection += new CustomizeColumnsCollectionEventHandler(generator_CustomizeColumnsCollection);
            generator.CustomizeColumn += new CustomizeColumnEventHandler(generator_CustomizeColumn);
            XtraReport report = generator.GenerateMVCReport(OperateSession.GetFromCache("AllowanceFileQuery") as MVCxGridViewState, zsxx);
            //  XtraReport report = generator.GenerateMVCReport(gridViewState, dt);

            //report.PaperKind = System.Drawing.Printing.PaperKind.A4;

            this.ViewData["rpt"] = report;

            return PartialView("ReportViewerPartial");

        }
        void generator_CustomizeColumnsCollection(object source, ColumnsCreationEventArgs e)
        {
            for (int i = 0; i < e.ColumnsInfo.Count; i++)
            {
               
                if (e.ColumnsInfo[i].FieldName == "FFileNo")
                {
                    e.ColumnsInfo[i].ColumnWidth = 80;
                }
                if (e.ColumnsInfo[i].FieldName == "NSRMC")
                {
                    e.ColumnsInfo[i].ColumnWidth =320 ;
                }
                if (e.ColumnsInfo[i].FieldName == "FFinancePropName")
                {
                    e.ColumnsInfo[i].ColumnWidth =100;

                }
                if (e.ColumnsInfo[i].FieldName == "FAmount")
                {
                    e.ColumnsInfo[i].ColumnWidth = 130;
                }
                if (e.ColumnsInfo[i].FieldName == "FFileName")
                {
                    e.ColumnsInfo[i].ColumnWidth = 400;
                }
               

            }
           
           
        }
        void generator_CustomizeColumn(object source, ControlCustomizationEventArgs e)
        {
            if (e.FieldName == "FAmount")
            {

                XRTableCell c = e.Owner as XRTableCell;
                c.TextAlignment = TextAlignment.MiddleRight;

            }

          
            if (e.FieldName == "NSRMC")
            {

                XRTableCell c = e.Owner as XRTableCell;
                c.TextAlignment = TextAlignment.MiddleLeft;

            }
        }
        public ActionResult Print()
        {
            GetFetch();
            // CreatReport(table);
            return View("Print");
        }
        public ActionResult ExportToXls()
        {
            string strwhere = GetFetch();


            BLL.AllowanceFileEntry allowanceFileEntryBLL = new BLL.AllowanceFileEntry();
            DataSet ds = allowanceFileEntryBLL.GetALList(strwhere);
            return GridViewExtension.ExportToXls(ExportGridViewSettings, ds.Tables[0], HttpUtility.UrlEncode("年度补贴预测数据导出.xls"), true);
        }

        private string GetFetch()
        {
            string fileNo = Fetch.Post("hidfileNo");
            string nsrmc = Fetch.Post("hidnsrmc");
            string financePropNo = Fetch.Post("hidfinancePropNo");
            string fileName = Fetch.Post("hidfileName");
            string amount1 = Fetch.Post("hidamount1");
            string amount2 = Fetch.Post("hidamount2");
            ViewData["fileNo"] = fileNo;
            ViewData["nsrmc"] = nsrmc;
            ViewData["financePropNo"] = financePropNo;
            ViewData["fileName"] = fileName;
            ViewData["amount1"] = amount1;
            ViewData["amount2"] = amount2;
            string strwhere = " 1=1 ";
            if (fileNo != "")
            {
                strwhere += " and FFileNo like  '%" + fileNo + "%'";
            }


            if (nsrmc != "")
            {
                strwhere += " and NSRMC like  '%" + nsrmc + "%'";
            }

            if (fileName != "")
            {
                strwhere += " and FFileName like  '%" + fileName + "%'";
            }

            if (financePropNo != "")
            {
                strwhere += " and FFinancePropNo ='" + financePropNo + "'";
            }

            if (amount1 != "" && amount2 != "")
            {
                strwhere += " and FAmount >=" + amount1 + " and FAmount<=" + amount2 + " ";
            }
            else if (amount1 != "")
            {
                strwhere += " and FAmount >=" + amount1;
            }
            else if (amount2 != "")
            {
                strwhere += " and FAmount <=" + amount2;
            }
            return strwhere;
        }


        static GridViewSettings exportGridViewSettings;
        public static GridViewSettings ExportGridViewSettings
        {
            get
            {
                if (exportGridViewSettings == null)
                    exportGridViewSettings = CreateExportGridViewSettings();
                return exportGridViewSettings;
            }
        }

        public static GridViewSettings CreateExportGridViewSettings()
        {
            var ss = new GridViewSettings();
            ss.Name = "gvEditing";
            ss.KeyFieldName = "FFileNo";
            
            ss.CallbackRouteValues = new { Controller = "AllowanceFileQuery", Action = "AllowanceFileQueryPartial" };
            ss.Width = System.Web.UI.WebControls.Unit.Percentage(100);
            ss.SettingsPager.PageSize = EnterpriseFund.Common.GlobalConstant.CONSTRING_PAGESIZE;
            //ss.Columns.Add("NSRDZDAH", "纳税人电子档案号");
            ss.Columns.Add("FFileNo", "文件号");
            ss.Columns.Add("NSRMC", "企业名称");
            ss.Columns.Add("FFinancePropName", "资金性质");
            ss.Columns.Add("FAmount", "补助金额(万元)");
            ss.Columns.Add("FFileName", "文件名称");
            ss.SettingsExport.Styles.Header.BackColor = System.Drawing.Color.White;
            ss.SettingsExport.Styles.Header.ForeColor = System.Drawing.Color.Black;
            ss.Settings.ShowGroupPanel = true;
            ss.Settings.ShowFooter = true;
            
            ss.SettingsLoadingPanel.Text = "数据读取中...";

            ss.GroupSummary.Add(DevExpress.Data.SummaryItemType.Sum, "FAmount").DisplayFormat = "合计:￥{0}";


            ss.ClientSideEvents.BeginCallback = "function(s,e){OnBeginCallback(s,e)}";
            ss.ClientSideEvents.EndCallback = "function(){}";
            ss.CommandColumn.Visible = false;
            ss.SettingsBehavior.EnableCustomizationWindow = true;
            ss.DataBinding = (sender, e) =>
            {
                ASPxGridView grid = (ASPxGridView)sender;
                for (int i = 0; i < grid.AllColumns.Count; i++)
                {
                    if (grid.AllColumns[i].HeaderStyle.HorizontalAlign == System.Web.UI.WebControls.HorizontalAlign.NotSet)
                    {
                        grid.AllColumns[i].HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                         
                    }
                }
            };
            ss.PreRender = ss.BeforeGetCallbackResult = (sender, e) =>
            {
                MVCxGridView gridView = sender as MVCxGridView;
                MVCxGridViewState gridViewState = new MVCxGridViewState(gridView);
                OperateSession.SetToCache(gridViewState, "AllowanceFileQuery");
            };

            
           
            return ss;
        }

        #endregion
    }
}
